<?php
require '../../vendor/autoload.php';

use utils\PathTool;
use utils\HttpHeader;

$head       = new HttpHeader();
$pathSvr    = $head->param("pathSvr");
$blockPath  = $head->param("blockPath");
$blockIndex = $head->param("blockIndex");//块索引，基于1
$blockOffset= $head->param("blockOffset");//基于块的偏移位置
$fileOffset = $head->param("fileOffset");//基于文件的偏移位置
$blockSize  = $head->param("blockSize");//当前需要下载的块大小
$pathSvr    = PathTool::url_decode($pathSvr);
//windows环境需要转成gbk编码,linux环境则不用
$pathSvr    = iconv("utf-8","gbk",$pathSvr);

header("Cache-Control: public");
header("Content-Type: application/octet-stream");
header("Content-Length: $blockSize");

$readSize = 5242880;//一次读5MB
$len      = intval($blockSize);
//文件不存在
if(!file_exists($pathSvr))
{
    header('HTTP/1.1 500 file not exist path:'.$pathSvr);//设置状态
    exit;
}
$fp       = fopen($pathSvr,"rb");
fseek($fp, $fileOffset);
while($len>0)
{
	if($readSize > $len) $readSize = $readSize - ($readSize - $len);	
	//reset time limit for big files
	set_time_limit(0);
	print(fread($fp,$readSize));
	$len -= $readSize;
	flush();
	ob_flush();
}
fclose($fp);
?>